<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
//exit();
$strWeb = $_SERVER[argv][1];
$strError = $strWeb."  (".date("Y-m-d H:i:s").")\n";
$strError .= "===========================================\n";

$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/SourceSite-utf8/secure";
include_once($_SERVER["DOCUMENT_ROOT"]."/config.inc.php");
include_once(__Modules_Path."/mysql_wrappers.lib.php");

//連結使用者資料中心
global $_ConnUser;
$strHost = "192.168.5.127";
$strDBUser = "kacino";
$strDBPass = "cyriac_0426";
$strDBName = "betLottoUser";

$arrErr = OtherDBConnection($_ConnUser, $strHost, $strDBUser, $strDBPass, $strDBName);
if(sizeof($arrErr) > 0){
	$strError .= "Err1: ".$arrErr["Subject"]."\n";
	$strError .= "ErrCon1: ".$arrErr["Content"]."\n";
	exit($strError);
}
/*
$arrErr = OtherDBConnection($_ConnUser, $strHost, $strDBUser, $strDBPass, $strDBName);
if(sizeof($arrErr) > 0){
	$strError .= "Err1: ".$arrErr["Subject"]."\n";
	$strError .= "ErrCon1: ".$arrErr["Content"]."\n";
	exit($strError);
}*/

//連結外部使用者資料中心
global $_ConnOuter;
$strOuterHost = $strWeb;//"114.202.247.120";
$strOuterDBUser = "ibkr";
$strOuterDBPass = "union28730808";
$strOuterDBName = "wgUData";

$arrErr = OtherDBConnection($_ConnOuter, $strOuterHost, $strOuterDBUser, $strOuterDBPass, $strOuterDBName);
if(sizeof($arrErr) > 0){
	$strError .= "Err1: ".$arrErr["Subject"]."\n";
	$strError .= "ErrCon1: ".$arrErr["Content"]."\n";
	exit($strError);
}


//從外部資料中心找出最新更新時間
$strSQL = "select `UpdateTime` from `outUserData` order by `UpdateTime` desc limit 1";
if(!$RS = mysql_query($strSQL, $_ConnOuter)){
	$strError .= "SQL: ".$strSQL."<br>\n";
	$strError .= "Error: ".mysql_error($_ConnOuter)."<br>\n";
	exit($strError);
}
$intUpdateTime = 0;
while($Rows = mysql_fetch_object($RS)){
	$intUpdateTime = $Rows -> UpdateTime - 180;
}

$intT = time();
while(true){
	//從資料中心 找出現有資料更新在此時間之後
	$strSQL = "select `UserAcc`, `EncodePwd` as UserPwd, `DBSite`, `UserGroup`, `UserStatus`, `SpGroup`, `SelfUpdateTime` as UpdateTime from `outUserData` where `SelfUpdateTime` > ".$intUpdateTime." order by `SelfUpdateTime`";
	if(!$RS = mysql_query($strSQL, $_ConnUser)){
		$strError .= "SQL: ".$strSQL."<br>\n";
		$strError .= "Error: ".mysql_error($_ConnUser)."<br>\n";
		exit($strError);
	}

	$arrUser = array();
	while($objUser = mysql_fetch_object($RS)){
		$arrUser[] = "('".$objUser -> UserAcc."', '".$objUser -> DBSite."', '".$objUser -> UserPwd."', '".$objUser -> UserGroup."', '".$objUser -> UserStatus."', '".$objUser -> SpGroup."', '".$objUser -> UpdateTime."', 0)";

		if($intUpdateTime < $objUser -> UpdateTime)$intUpdateTime = $objUser -> UpdateTime;

		$intNo = sizeof($arrUser);
		if($intNo >= 2000){
			$strError .= "\nNo => ".$intNo."\n";

			$strSQL = "insert into `outUserData`(`UserAcc`, `DBSite`, `UserPwd`, `UserGroup`, `UserStatus`, `SpGroup`, `UpdateTime`, `ComID`) values";
			$strSQL .= implode(", ", $arrUser);
			$strSQL .= "ON DUPLICATE KEY UPDATE UserPwd = VALUES(UserPwd), UserGroup = VALUES(UserGroup), UserStatus = VALUES(UserStatus), SpGroup = VALUES(SpGroup), UpdateTime = VALUES(UpdateTime)";

			if(!mysql_query($strSQL, $_ConnOuter)){
				$strError .= "SQL: ".$strSQL."<br>\n";
				$strError .= "Error: ".mysql_error($_ConnOuter)."<br>\n";
			}
			$arrUser = array();
		}
	}

	$intNo = sizeof($arrUser);
	$strError .= "\nNo => ".$intNo."\n";

	if($intNo > 0){
		$strSQL = "insert into `outUserData`(`UserAcc`, `DBSite`, `UserPwd`, `UserGroup`, `UserStatus`, `SpGroup`, `UpdateTime`, `ComID`) values";
		$strSQL .= implode(", ", $arrUser);
		$strSQL .= "ON DUPLICATE KEY UPDATE UserPwd = VALUES(UserPwd), UserGroup = VALUES(UserGroup), UserStatus = VALUES(UserStatus), SpGroup = VALUES(SpGroup), UpdateTime = VALUES(UpdateTime)";

		if(!mysql_query($strSQL, $_ConnOuter)){
			$strError .= "SQL: ".$strSQL."<br>\n";
			$strError .= "Error: ".mysql_error($_ConnOuter)."<br>\n";
		}
	}

	if(time() - $intT >= 55)break;
	$strError .= "sleep => 5\n";
	sleep(5);
}
print $strError."\nEnd (".date("Y-m-d H:i:s").")\n";
?>
